package com.qf.commerce.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.commerce.common.vo.AreaVo;
import com.qf.commerce.entity.Area;
import com.qf.commerce.mapper.AreaMapper;
import com.qf.commerce.service.AreaService;
import com.qf.commerce.utils.CommonBeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
/**
 * @author Administrator
 */
@Service
public class AreaServiceImpl implements AreaService {
    @Resource
    AreaMapper areaMapper;

    public StringBuffer getDeliveryRule(Area area,Integer freightSetting,Integer pricingManner){
        StringBuffer dr=new StringBuffer();
        if(freightSetting==2){
            if(area.getAreaType()==2){
                dr.append("满"+area.getBaseFreight()+"元包邮");
            }else {
                dr.append("不能下单地区");
            }
        }else if(freightSetting==1){
            if(area.getAreaType()==1){
                if(pricingManner==1){
                    dr.append(area.getBaseValue()+"件以内,"+area.getBaseFreight()+"元,每增加"+area.getAdditionalValue()
                            +"件,增加运费"+area.getAdditionalFreight()+"元");
                }
                else{
                    dr.append(area.getBaseValue()+"kg以内,"+area.getBaseFreight()+"元,每增加"+area.getAdditionalValue()
                            +"kg,增加运费"+area.getAdditionalFreight()+"元");
                }
            }else if(area.getAreaType()==2){
                dr.append("满"+area.getBaseFreight()+"元包邮");
            }else {
                dr.append("不能下单地区");
            }
        }
        return dr;
    }
    @Override
    public int addArea(Area area,Integer freightSetting,Integer pricingManner) {
        area.setDeliveryRule(getDeliveryRule(area,freightSetting,pricingManner).toString());
        return areaMapper.insert(area);
    }

    @Override
    public int modifyArea(Area area,Integer freightSetting,Integer pricingManner) {
        area.setDeliveryRule(getDeliveryRule(area,freightSetting,pricingManner).toString());
        return areaMapper.update(area);
    }

    @Override
    public int deleteArea(Long templateId) {
        return areaMapper.delete(templateId);
    }

    @Override
    public int deleteAreas(List<Long> areaIds) {
        return areaMapper.deletes(areaIds);
    }

    @Override
    public PageInfo<AreaVo> queryAreaList() {
        return CommonBeanUtils.copyPage(PageHelper.startPage(1,10).doSelectPageInfo(()->areaMapper.selectList()),
                new PageInfo<AreaVo>(),AreaVo::new);
    }


}
